package com.b2c.ftysys.action;

import java.io.File;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.b2c.ftysys.constants.FtySysConstants;
import com.b2c.ftysys.domain.B2cFsyWdExample;
import com.b2c.ftysys.domain.B2cFsyWs;
import com.b2c.ftysys.domain.ExampleFactory;
import com.b2c.ftysys.domain.PageResult;
import com.b2c.ftysys.helper.ObjectUtils;
import com.b2c.ftysys.sevice.RouteService;
import com.b2c.ftysys.sevice.ServicesException;
import com.b2c.ftysys.sevice.WsInfoService;
import com.b2c.ftysys.vo.RouteSettingVo;

@Controller
public class RouteAction {

	@Autowired
	WsInfoService wsInofservice = null;

	@Autowired
	RouteService routeService = null;

	@Autowired
	private ServletContext servletContext = null;

	@RequestMapping("/route")
	public String toWsRoutePage(HttpServletRequest request) {
		request.setAttribute("dataList", wsInofservice.queryAllWs());
		request.setAttribute("routeTypes", routeService.getAllRouteType());
		return "route/route";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/route")
	public String saveWsRoute(HttpServletRequest request) {
		String[] datas = request.getParameterValues("routeSname");
		String routeType = request.getParameter("routeType");

		int resultCode = routeService.regRoute(routeType, datas);

		request.setAttribute("resultCode", resultCode);
		request.setAttribute("routeType", routeType);

		return "redirect:/route.do";
	}

	@RequestMapping("/ws")
	public String toWsPage(HttpServletRequest request) {
		B2cFsyWs ws = new B2cFsyWs();
		request.setAttribute("dataList", wsInofservice.queryWsListByClause(ws));
		request.setAttribute("ws", ws);
		return "route/ws";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/ws")
	public String toWsPage(B2cFsyWs ws, HttpServletRequest request) {
		if (null == ws)
			ws = new B2cFsyWs();
		request.setAttribute("dataList", wsInofservice.queryWsListByClause(ws));
		request.setAttribute("ws", ws);
		return "route/ws";
	}

	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = "routeSetting")
	public String toRouteSettingPage(HttpServletRequest request)
			throws Exception {
		RouteSettingVo vo = (RouteSettingVo) ObjectUtils
				.ConvertURLRequsetMapToObj(request.getParameterMap(),
						RouteSettingVo.class);

		PageResult page = new PageResult();
		if (null != vo.getPageSize() && vo.getPageSize() != 0) {
			page.setPageSize(vo.getPageSize());
		}

		if (null != vo.getCurPage() && vo.getCurPage() != 0) {
			page.setCurPage(vo.getCurPage());
		}

		B2cFsyWdExample example = ExampleFactory.createWdExample();
		example.createCriteria(vo);

		if (null != vo.getOrderBy()) {
			example.setOrderByClause(ObjectUtils.getOrderBy(vo.getOrderBy(), vo
					.getACode()));
		}else{
			example.setOrderByClause(" create_time desc ");
		}

		page.setSqlParamObj(example);

		request.setAttribute("routeTypes", routeService.getAllRouteType());
		request.setAttribute("page", routeService.getRouteSettingByPage(page));
		request.setAttribute("vo", vo);
		return "route/routeSetting";
	}

	@RequestMapping(method = RequestMethod.POST, value = "importRouteSetting")
	public String importRouteSetting(HttpServletRequest request,
			@RequestParam("excelFile")
			CommonsMultipartFile mFile) throws ServicesException {

		if (!mFile.isEmpty()) {
			String path = servletContext.getRealPath("/")
					+ FtySysConstants.ROUTE_SETTING_IMPORT_EXCEL_FILE_PREFIX; // 获取本地存储路径
			String orderFileName = new Date().getTime()
					+ FtySysConstants.IMPORT_EXCEL_FILE_SUBFIX;
			File file = new File(path + orderFileName); // 新建一个文件
			try {
				mFile.getFileItem().write(file);
				request.setAttribute("iNum", routeService
						.importRouteSetting(file.getAbsolutePath()));
			} catch (Exception e) {
				e.printStackTrace();
				throw new ServicesException(e);
			}
		}
		return "redirect:/routeSetting.do";
	}
}
